/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac4.servidor;

import edu.uoc.tdp.pac4.bbdd.GestorAdministrativaBBDD;
import edu.uoc.tdp.pac4.beans.Client;
import edu.uoc.tdp.pac4.beans.ClientVehicle;
import edu.uoc.tdp.pac4.beans.Peca;
import edu.uoc.tdp.pac4.beans.Solicitud;
import edu.uoc.tdp.pac4.beans.Stock;
import edu.uoc.tdp.pac4.beans.StockPeca;
import edu.uoc.tdp.pac4.beans.Vehicle;
import edu.uoc.tdp.pac4.comun.GestorAdministrativaInterface;
import edu.uoc.tdp.pac4.comun.PAC4Exception;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author mirica
 */
public class GestorAdministrativaImpl extends UnicastRemoteObject implements GestorAdministrativaInterface, Serializable {
    
    private static final long serialVersionUID = 1L;

    private GestorAdministrativaBBDD gestorAdministrativaBBDD;
    
    public GestorAdministrativaImpl() throws RemoteException, PAC4Exception {
        super();
        this.gestorAdministrativaBBDD = new GestorAdministrativaBBDD();
    }
    
    @Override
    public ClientVehicle obtenerCliente(String nif) throws RemoteException, PAC4Exception{
        return gestorAdministrativaBBDD.getCliente(nif);
    }
    
    @Override
    public void actualizarClienteVehiculo (ClientVehicle clientVehicle) throws RemoteException, PAC4Exception{
        Client client = MapearCliente(clientVehicle);
        gestorAdministrativaBBDD.updateClient(client);
        
        Vehicle vehicle = MapearVehicle(clientVehicle);
        gestorAdministrativaBBDD.updateVehicle(vehicle);
    }
    
    @Override
    public ClientVehicle insertarClienteVehiculo (ClientVehicle clientVehicle) throws RemoteException, PAC4Exception{
        int IdVehicle = 0;
        Vehicle vehicle = MapearVehicle(clientVehicle);
        IdVehicle = gestorAdministrativaBBDD.insertVehicle(vehicle);
        clientVehicle.setIdVehicle(IdVehicle);
        
        Client client = MapearCliente(clientVehicle);
        int numClient = gestorAdministrativaBBDD.insertClient(client);
        clientVehicle.setNumCliente(numClient);
        
        return clientVehicle;
    }

    private Client MapearCliente (ClientVehicle clientVehicle) {
        Client client = new Client();
        client.setAdresa(clientVehicle.getAdresa());
        client.setCodiPostal(clientVehicle.getCodiPostal());
        client.setCognoms(clientVehicle.getCognoms());
        client.setDataAlta(clientVehicle.getDataAlta());
        client.setNif(clientVehicle.getNif());
        client.setNom(clientVehicle.getNom());
        client.setNumCliente(clientVehicle.getNumCliente());
        client.setNum_chasis(clientVehicle.getNum_chasis());
        client.setPoblacio(clientVehicle.getPoblacio());    
        client.setIdVehicle(clientVehicle.getIdVehicle());
        
        return client;
    }
    
    private Vehicle MapearVehicle (ClientVehicle clientVehicle) {
        Vehicle vehicle = new Vehicle();
        vehicle.setAnio(clientVehicle.getAnio());
        vehicle.setColor(clientVehicle.getColor());
        vehicle.setMarca(clientVehicle.getMarca());
        vehicle.setMatricula(clientVehicle.getMatricula());
        vehicle.setModel(clientVehicle.getModel());
        vehicle.setNumReparacio(clientVehicle.getNumReparacio());
        vehicle.setNum_chasis(clientVehicle.getNum_chasis());
        vehicle.setTipus(clientVehicle.getTipus());
        vehicle.setIdVehicle(clientVehicle.getIdVehicle());
        
        return vehicle;
    }

    @Override
    public Solicitud obtenerSolicitud (int numSolicitud) throws RemoteException, PAC4Exception {
        return gestorAdministrativaBBDD.getSolicitud(numSolicitud);
    }
    
    @Override
    public Solicitud actualizarSolicitud (Solicitud solicitud) throws RemoteException, PAC4Exception {
        gestorAdministrativaBBDD.updateSolicitud(solicitud);
        return gestorAdministrativaBBDD.getSolicitud(solicitud.getNumSol());
    }
    
    @Override
    public Solicitud insertarSolicitud (Solicitud solicitud) throws RemoteException, PAC4Exception {
        int numSol = 0;
        numSol = gestorAdministrativaBBDD.insertSolicitud(solicitud);
        
        return gestorAdministrativaBBDD.getSolicitud(numSol);
    }
    
    @Override
    public ArrayList<Peca> obtenerPiezas () throws RemoteException, PAC4Exception {
        return gestorAdministrativaBBDD.getPecas();
    }
             
    @Override
    public ArrayList<Stock> obtenerStock (int idTaller) throws RemoteException, PAC4Exception {
        ArrayList<Stock> result = new ArrayList<Stock>();
        
        ArrayList<Peca> listPeca = gestorAdministrativaBBDD.getPecas();
        Iterator it = listPeca.iterator();
        
        while (it.hasNext()) {
            Peca p = (Peca)it.next();
            StockPeca sp = gestorAdministrativaBBDD.getStockPeca(p.getCodiPeca(), idTaller);
            
            Stock s = new Stock();
            s.setCodiPeca(p.getCodiPeca());
            s.setDescripcio(p.getDescripcio());
            s.setPvd(p.getPvd());
            s.setPvp(p.getPvp());
            
            if(sp != null) {
                s.setIdTaller(sp.getIdTaller());
                s.setStock(sp.getStock());
            }

            result.add(s);
        }         
        
        return result;
    }
    
    @Override
    public void ActualizarStock(int codiPeca, int idTaller, int unidades) throws RemoteException, PAC4Exception {           
        StockPeca sp = gestorAdministrativaBBDD.getStockPeca(codiPeca, idTaller);
        
        if(sp!=null) {
            //UPDATE
            sp.setStock(unidades);
            gestorAdministrativaBBDD.updateStockPeca(sp);
            sp.getIdStockPeca();
        }
        else {
            //INSERT
            sp = new StockPeca();
            sp.setCodiPeca(codiPeca);
            sp.setIdTaller(idTaller);
            sp.setStock(unidades);
            gestorAdministrativaBBDD.insertStockPeca(sp);
        }
    }
}